package top.lyjwn.todo.common.utils;

import javax.sql.rowset.serial.SerialClob;
import javax.sql.rowset.serial.SerialException;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.Reader;
import java.sql.Clob;
import java.sql.SQLException;

public class LobUtil {
    /**
     * clob 转string
     *
     * @param clob
     * @return
     */
    public static String Clob2String(Clob clob) {
        String reString = "";
        try {
            Reader is = clob.getCharacterStream();// 得到流
            BufferedReader br = new BufferedReader(is);
            String s = br.readLine();
            StringBuffer sb = new StringBuffer();
            while (s != null) {// 执行循环将字符串全部取出付值给StringBuffer由StringBuffer转成STRING
                sb.append(s);
                s = br.readLine();
            }
            reString = sb.toString();
        } catch (SQLException | IOException e) {
            e.printStackTrace();
        }
        return reString;


    }

    /**
     * 字符串转clob
     *
     * @param s
     * @return
     */
    public static Clob String2Clob(String s) {
        try {
            return new SerialClob(s.toCharArray());
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }


    }

}
